package com.fadadianqi.logistics.sell.dao.ibatis;

import com.fadadianqi.commons.GenericDAOSuport;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.logistics.sell.dao.ItemDAO;
import com.fadadianqi.logistics.sell.model.Item;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("billItemDAO")
public class ItemDAOImpl extends GenericDAOSuport<String, Item> implements
        ItemDAO {

    @Override
    protected Class<?> getObjectClass() {
        return Item.class;
    }

    @Override
    protected String getTableName() {
        return "bill_item";
    }

    @Override
    public List<Item> findByBatchId(String batchId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_BATCH_ID, batchId);
        return super.findByPropertys(map);
    }

    @Override
    public List<Item> findByInventoryItemId(String inventory_item_id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_INVENTORY_ITEM_ID, inventory_item_id);
        return super.findByPropertys(map);
    }


    @Override
    public List<Item> findByBillId(String billId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_BILL_ID, billId);
        return super.findByPropertys(map);
    }

    @Override
    public int countBetween(Category category, Date date1, Date date2) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date1", date1);
        map.put("date2", date2);
        map.put(Item.PROP_CATEGORY_ID, category.getId());
        Integer i = (Integer) getSqlMapClientTemplate().queryForObject(getNamespace() + ".countBetween", map);
        if (i == null) {
            return 0;
        }
        return i;
    }

    @Override
    public int countNotReturn(String billId) {
        Integer i = (Integer) getSqlMapClientTemplate().queryForObject(getNamespace() + ".countNotReturn", billId);
        if (i == null) {
            return 0;
        }
        return i;
    }

    @Override
    public double sumPayablesBetween(Category category, Date date1, Date date2) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date1", date1);
        map.put("date2", date2);
        map.put(Item.PROP_CATEGORY_ID, category.getId());
        Double d = (Double) getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumPayablesBetween", map);
        if (d == null) {
            return 0D;
        }
        return d;
    }

    @Override
    public double sumCostBetween(Category category, Date date1, Date date2) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date1", date1);
        map.put("date2", date2);
        map.put(Item.PROP_CATEGORY_ID, category.getId());
        Double d = (Double) getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumCostBetween", map);
        if (d == null) {
            return 0D;
        }
        return d;
    }

    @Override
    public Item getItemByGoodsIdAndBillId(String goodsId, String billId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Item.PROP_GOODS_ID, goodsId);
        map.put(Item.PROP_BILL_ID, billId);
        return super.getByPropertys(map);
    }

    @Override
    public Double sumPurchaseBetween(Date start, Date end) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date1", start);
        map.put("date2", end);
        Double d = (Double) getSqlMapClientTemplate().queryForObject(getNamespace() + ".sumPurchaseBetween", map);
        if (d == null) {
            return 0D;
        }
        return d;
    }


}
